package by.grsu.mikhailava.page.tutor;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.mikhailava.app.TestWebApplication;
import by.grsu.mikhailava.dao.QuestionDao;
import by.grsu.mikhailava.entity.Question;
import by.grsu.mikhailava.entity.Subject;

public class QuestionsPage extends WebPage{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3608576684809242092L;

	@SuppressWarnings({ "serial", "rawtypes" })
	public QuestionsPage(Subject subject){
		super();
		final Subject subject1 = subject;

		// retrieve new instance of DAO (can be reused)
		final QuestionDao circleDao = new QuestionDao(TestWebApplication.DATA_XML_FOLDER);

		// get list of Circle objects using DAO
		final List<Question> allQuestion = circleDao.getAll();

		// create "repeater" component to build rows of the "table" in output HTML
		add(new DataView<Question>("table-rows", new ListDataProvider<Question>(allQuestion)) {
			
			@Override
			protected void populateItem(final Item<Question> item) {
				final Question circle = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("id", circle.getId()));
				item.add(new Label("questionName", circle.getQuestionName()));
				item.add(new Label("subjectId", circle.getSubjectId()));

				// add button component to open "edit" page
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						// open "edit" page for specified Circle
						setResponsePage(new AnswerPage(circle));
					}
				});

				// add button component to delete Circle
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						// execute "delete" operation
						circleDao.delete(circle.getId());
						// reload page
						setResponsePage(new QuestionsPage(subject1));
					}
				});
			}
		});

		// add button component to open "edit" page
		add(new Link("createSubject") {
			@Override
			public void onClick() {
				// open "edit" page for NEW Circle
				setResponsePage(new QuestionEditPage(subject1));
			}
		});
		
	}
	

}
